<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!--edge浏览器H5兼容设置-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--360浏览器H5兼容设置-->
    <meta name="renderer" content="webkit"/>
    <title>电脑商城</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--导入核心文件-->
    <script src="../bootstrap3/js/holder.js"></script>
    <link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="../bootstrap3/jquery-1.9.1.min.js"></script>
    <script src="../bootstrap3/js/bootstrap.js"></script>
    <!-- 字体图标 -->
    <link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css"/>
    <link rel="stylesheet" type="text/css" href="../css/layout.css"/>
    <link rel="stylesheet" type="text/css" href="../css/top.css"/>
    <link rel="stylesheet" type="text/css" href="../css/footer.css"/>
    <link rel="stylesheet" type="text/css" href="../css/order.css"/>
</head>

<body>
<!--头部-->
<header class="header">
    <!--电脑商城logo-->
    <div class="row">
        <div class="col-md-3">
            <a href="index_user.html">
                <img src="../images/index/stumalllogo.png"/>
            </a>
        </div>
        <!--快捷选项-->
        <div class="col-md-9 top-item">
            <ul class="list-inline pull-right">

                <li><a href="orders_user.html"><span class="fa fa-file-text"></span>&nbsp;订单</a></li>
                <li class="li-split">|</li>
                <li><a href="cart_user.html"><span class="fa fa-cart-plus"></span>&nbsp;购物车</a></li>
                <li class="li-split">|</li>
                <li>
                    <!--下列列表按钮 ：管理-->
                    <div class="btn-group">
                        <button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">
                            <span id="top-dropdown-btn"><span class="fa fa-gears"></span>&nbsp;管理 <span
                                    class="caret"></span></span>
                        </button>
                        <ul class="dropdown-menu top-dropdown-ul" role="menu">
                            <li><a href="password.html">修改密码</a></li>
                            <li><a href="userdata.html">个人资料</a></li>
                            <li><a href="upload.html">上传头像</a></li>
                            <li><a href="address.html">收货管理 </a></li>
                        </ul>
                    </div>
                </li>
                <li class="li-split">|</li>
                <li><a href="login.html"><span class="fa fa-user"></span>&nbsp;登录</a></li>
            </ul>
        </div>
    </div>
</header>
<!--导航 -->
<!--分割导航和顶部-->
<div class="row top-nav">
    <div class="col-md-6">
        <ul class="nav nav-pills">
            <li>
                <a href="#"></a>
            </li>
            <li class="active"><a href="index_user.html"><span class="fa fa-home"></span></a></li>
            <form id="form-search" action="search_user.html" class="form-inline pull-right" role="form">
                <div class="form-group">
                    <input type="text" class="form-control" id="search" name="title" placeholder="请输入商品名称进行搜索">
                </div>
                <input id="btn-search" type="submit" class="btn btn-primary" value="搜索"/>
            </form>
        </ul>
    </div>
</div>
<!--头部结束-->
<!--导航结束-->
<div class="container">
    <div class="col-md-2">
        <!--左侧导航开始-->
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <!--主选项：我的订单-->
                    <p class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">我的订单</a>
                    </p>
                </div>
                <div id="collapseOne" class="panel-collapse collapse in">
                    <div class="panel-body">
                        <div><a href="orders_user.html"><b>全部订单</b></a></div>
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <!--主选项：资料修改-->
                    <p class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">资料修改</a>
                    </p>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse">
                    <div class="panel-body">
                        <div><a href="password.html"><b>修改密码</b></a></div>
                        <div><a href="userdata.html">个人资料</a></div>
                        <div><a href="upload.html">上传头像</a></div>
                        <div><a href="address.html">收货管理</a></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--左侧导航结束-->
    <div class="col-md-10">
        <div class="panel panel-default">
            <table class="cart-table" width="100%">
                <thead>
                <th><input id="refuseReason" type="text" class="form-control"
                           placeholder="此处为您最近所退货物退货原因" readonly="readonly"></th>
                </thead>
            </table>
            <div class="panel-body">
                <form action="orderInfo.html" role="form">
                    <table class="orders-table" width="101%">
                        <thead>
                        <tr>
                            <th width="5%">用户id</th>
                            <th width="5%">商家id</th>
                            <th width="5%"></th>
                            <th width="30%">商品</th>
                            <th width="10%">单价</th>
                            <th width="10%">数量</th>
                            <th width="10%">小计</th>
                            <th width="8%">状态</th>
                            <th width="20%">操作(请已到货后收货, 切勿重复收货)</th>
                        </tr>
                        </thead>
                        <tbody id="order-list" class="orders-body">
                        <tr>
                            <td>1</td>
                            <td><img src="../images/portal/12DELLXPS13-silvery/collect.png" class="img-responsive"/>
                            </td>
                            <td>联想(Lenovo)小新Air13 Pro 13.3英寸14.8mm超轻薄笔记本电脑</td>
                            <td>¥<span>5298</span></td>
                            <td>1件</td>
                            <td>¥<span>5298</span></td>
                            <td><a href="#">申请售后</a></td>
                            <td>
                                <div><input type="submit" name="uid" value="#{uid}">订单详情</a></div>
                            </td>
                            <td><a href="#" class="btn btn-default btn-xs">收货</a></td>
                        </tr>
                        </tbody>
                    </table>

                </form>

                <div>
                    <span id="all-price" class="pull-right">123</span><span class="pull-right">总金额:</span>
                </div>
            </div>
        </div>

    </div>
</div>
<script type="text/javascript">

    $(document).ready(function () {
        showOrderList();
    });

    function showOrderList() {
        $("#order-list").empty();
        $.ajax({
            url: "/order/listbu",
            type: "GET",
            dataType: "JSON",
            success: function (json) {
                console.log(json.data);
                let list = json.data;
                console.log("count=" + list.length);
                let allPrice = 0;
                for (let i = 0; i < list.length; i++) {
                    let tr = '<tr>\n' +
                        '<td>#{uid}</td>' +
                        '<td>#{businessId}</td>' +
                        '                         <td><img src="..#{image}collect.png" class="img-responsive" /></td>' +
                        '                        <td>#{title}</td>' +
                        '                        <td>¥<span>#{price}</span></td>' +
                        '                        <td>#{num}</td>\n' +
                        '                        <td>¥<span>#{prices}</span></td>' +
                        '                        <td>\n' +
                        '                            <div ">#{pStatus}</div>\n' +
                        '                        </td>\n' +
                        '<td><input id="btn-send" onclick="sendUidBid(#{uid},#{businessId}),    businessAddMoney(#{prices},#{businessId},#{oid},#{pid},#{pSt}),receiveProduct(#{oid},#{pid},#{num},#{pSt})" class="btn btn-primary" type="button" value="收货" />' +
                        '<input id="btn-return" onclick="returnProduct(#{oid},#{pid},#{pSt},#{payDate})" class="btn btn-primary" type="button" value="退货" /></td>\n' +
                        '                    </tr>';

                    tr = tr.replace(/#{uid}/g,"'"+list[i].uid+"'");
                    tr = tr.replace(/#{businessId}/g,"'"+list[i].businessId+"'");
                    tr = tr.replace(/#{oid}/g, "'"+list[i].oid+"'");
                    tr = tr.replace(/#{pid}/g, "'"+list[i].pid+"'");
                    tr = tr.replace(/#{image}/g, list[i].image);
                    tr = tr.replace(/#{title}/g, list[i].title);
                    tr = tr.replace(/#{price}/g, list[i].price);
                    tr = tr.replace(/#{num}/g, list[i].num);
                    tr = tr.replace(/#{prices}/g, list[i].price * list[i].num);
                    tr=tr.replace(/#{payDate}/g,"'"+list[i].payTime+"'");

                    console.log(list[i]);
                    // 5申请退货 6退货完成 7退货驳回
                    if (list[i].pstatus == 0) {
                        tr = tr.replace(/#{pStatus}/g, "未发货");
                        tr = tr.replace(/#{pSt}/g, 0);
                    } else if (list[i].pstatus == 1) {
                        tr = tr.replace(/#{pStatus}/g, "已发货");
                        tr = tr.replace(/#{pSt}/g, 1);
                    } else if (list[i].pstatus == 2) {
                        tr = tr.replace(/#{pStatus}/g, "已到货");
                        tr = tr.replace(/#{pSt}/g, 2);
                    } else if (list[i].pstatus == 3) {
                        tr = tr.replace(/#{pStatus}/g, "已收货");
                        tr = tr.replace(/#{pSt}/g, 3);
                    } else if (list[i].pstatus == 4) {
                        tr = tr.replace(/#{pStatus}/g, "交易完成");
                        tr = tr.replace(/#{pSt}/g, 4);
                    } else if (list[i].pstatus == 5) {
                        tr = tr.replace(/#{pStatus}/g, "申请退货");
                        tr = tr.replace(/#{pSt}/g, 5);
                    } else if (list[i].pstatus == 6) {
                        tr = tr.replace(/#{pStatus}/g, "退货完成");
                        tr = tr.replace(/#{pSt}/g, 6);
                    } else if (list[i].pstatus == 7) {
                        tr = tr.replace(/#{pStatus}/g, "退货驳回");
                        tr = tr.replace(/#{pSt}/g, 7);
                        $("#refuseReason").val(localStorage.getItem("reason"));
                    } else {
                        tr = tr.replace(/#{pStatus}/g, "订单异常");
                    }
                    $("#order-list").append(tr);
                    allPrice += list[i].price * list[i].num;
                }
                $("#all-price").html(allPrice);
            }
        });
    }

    function sendUidBid(uid, bid) {
        localStorage.removeItem("bid");
        localStorage.setItem("bid", "'"+bid+"'");
        localStorage.removeItem("uid");
        localStorage.setItem("uid", "'"+uid+"'");
        console.log("'"+localStorage.getItem("uid")+"'");
        console.log("'"+localStorage.getItem("bid")+"'");
    }
    // businessAddMoney(#{prices},#{businessId})
    function receiveProduct(oid, pid, num,pStatus) {
        if (pStatus==1||pStatus==2){
            $.ajax({
                url: "/order/receive_product",
                type: "POST",
                data: "oid=" + oid + "&pid=" + pid,
                dataType: "JSON",
                success: function (json) {
                    if (json.state == 200) {
                        // console.log("货物状态"+json.data);
                        alert("收货成功");
                        // businessAddMoney();
                        addPriority(oid, pid, num);
                        showOrderList();
                        alert("请前往商品界面评价");
                        // console.log(pid);
                        localStorage.removeItem("oid");
                        localStorage.removeItem("pid");
                        localStorage.setItem("oid","'"+oid+"'");
                        localStorage.setItem("pid", "'"+pid+"'");
                        location.href = "product_user.html";
                    } else {
                        alert("货物异常");
                        showOrderList();
                    }
                },
                error: function (xhr) {
                    alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
                }
            })
        }else if(pStatus==0){
            alert("商品未发货，请耐心等待");
        }else if(pStatus==3){
            alert("商品已收货，请勿重复收货");
        }else if(pStatus==4){
            alert("交易完成，无法收货");
        }else if(pStatus==5||pStatus==6||pStatus==7){
            alert("商品正在退货中，无法收货");
        }

    }

    function returnProduct(oid, pid, pStatus,payDate) {
        //获取下单日期的日和小时
        var Date_payDay= payDate.substr(8,8);
        var Date_payHours= payDate.substr(11,12);
        // console.log("支付日期"+sDate2_Day);
        console.log("支付日期"+Date_payDay.substring(0,2));
        console.log("支付小时数"+Date_payHours.substring(0,2));
        //获取当前日期的日和小时
        let nowDate=new Date();
        var Date_nowDay=nowDate.getDate()
        var Date_nowHours=nowDate.getHours();
        // console.log("当前时间"+nowDate);
        console.log("当前日期"+Date_nowDay);
        console.log("当前小时数"+Date_nowHours);
        var day=parseInt(Date_nowDay)-parseInt(Date_payDay) ;
        var time=parseInt(Date_nowHours)-parseInt(Date_payHours);
        console.log(day);
        console.log(time);

        if(day<1&&time<=24){
            if (pStatus == 0 || pStatus == 1 || pStatus == 2||pStatus == 3||pStatus == 4) {
                $.ajax({
                    url: "/order/return_product",
                    type: "POST",
                    data: "oid=" + oid + "&pid=" + pid,
                    dataType: "JSON",
                    success: function (json) {
                        if (json.state == 200) {
                            alert("申请退货中，等待管理员审核");
                            showOrderList();
                        } else {
                            alert("货物异常");
                        }
                    },
                    error: function (xhr) {
                        alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
                    }
                })
            }else if (pStatus == 5) {
                alert("订单已经申请退货，请耐心等待商家审核");
            }else if (pStatus == 6) {
                alert("订单已经退货完成，请勿重复退货");
            }else if (pStatus == 7) {
                alert("订单拒绝退货，若有异议请联系管理员");
            }
        }else{
            alert("订单已经超过24小时，无法退货，若有异议请联系管理员");
        }
    }


    function addPriority(oid, pid, num) {
        $.ajax({
            url: "/order/business_addPriority",
            type: "POST",
            data: "oid=" + oid + "&pid=" + pid + "&num=" + num,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    console.log("销量增加");
                    // alert("销量增加");
                } else {
                    alert("货物异常");
                }
            },
            error: function (xhr) {
                alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function getLevelByBid(businessId) {
        $.ajax({
            url: "/user/get_levelByBid",
            type: "POST",
            data: "businessId=" + businessId,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    console.log("商家等级：" + json.data.level);
                    if (location.href.indexOf("#reloaded") == -1) {
                        location.href = location.href + "#reloaded";
                        location.reload();
                    }
                    // tax=parseInt(json.data.level);
                    // console.log(tax);
                    // return tax;
                    let level = parseInt(json.data.level);
                    localStorage.setItem("level", level);
                } else {
                    alert("查询失败");
                }
            },
            error: function (xhr) {
                alert("商家等级产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })

    }

    function businessAddMoney(prices, businessId,oid,pid,pStatus) {
        // let pStatus_t=pStatus(oid,pid);
        console.log("pStatus!!!!:"+pStatus);

        if (pStatus==1||pStatus==2){
            console.log("扣税前商家金额" + prices);
            getLevelByBid(businessId);
            // console.log("商家等级1111111111："+getLevelByBid(businessId));
            let tax = localStorage.getItem("level");
            if (tax == 5) {
                prices = parseInt(prices -prices*0.001);
                console.log("扣税后商家金额" + prices);
                $.ajax({
                    url: "/user/businessAddMoney",
                    type: "POST",
                    data: "prices=" + prices + "&businessId=" + businessId,
                    dataType: "JSON",
                    success: function (json) {
                        if (json.state == 200) {
                            alert("商家付款成功");
                        } else {
                            alert("充值失败");
                        }
                    },
                    error: function (xhr) {
                        alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
                    }
                })
            } else if (tax == 4) {
                prices =parseInt(prices -prices*0.002) ;
                console.log("扣税后商家金额" + prices);
                $.ajax({
                    url: "/user/businessAddMoney",
                    type: "POST",
                    data: "prices=" + prices + "&businessId=" + businessId,
                    dataType: "JSON",
                    success: function (json) {
                        if (json.state == 200) {
                            alert("商家付款成功");
                        } else {
                            alert("充值失败");
                        }
                    },
                    error: function (xhr) {
                        alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
                    }
                })
            } else if (tax == 3) {
                prices =parseInt(prices -prices*0.005) ;
                console.log("扣税后商家金额" + prices);
                $.ajax({
                    url: "/user/businessAddMoney",
                    type: "POST",
                    data: "prices=" + prices + "&businessId=" + businessId,
                    dataType: "JSON",
                    success: function (json) {
                        if (json.state == 200) {
                            alert("商家付款成功");
                        } else {
                            alert("充值失败");
                        }
                    },
                    error: function (xhr) {
                        alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
                    }
                })
            } else if (tax == 2) {
                prices =parseInt(prices -prices*0.0075) ;
                console.log("扣税后商家金额" + prices);
                $.ajax({
                    url: "/user/businessAddMoney",
                    type: "POST",
                    data: "prices=" + prices + "&businessId=" + businessId,
                    dataType: "JSON",
                    success: function (json) {
                        if (json.state == 200) {
                            alert("商家付款成功");
                        } else {
                            alert("充值失败");
                        }
                    },
                    error: function (xhr) {
                        alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
                    }
                })
            } else if (tax == 1) {
                prices =parseInt(prices -prices*0.01) ;
                console.log("扣税后商家金额" + prices);
                $.ajax({
                    url: "/user/businessAddMoney",
                    type: "POST",
                    data: "prices=" + prices + "&businessId=" + businessId,
                    dataType: "JSON",
                    success: function (json) {
                        if (json.state == 200) {
                            alert("商家付款成功");
                        } else {
                            alert("充值失败");
                        }
                    },
                    error: function (xhr) {
                        alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
                    }
                })
            }
        }else if(pStatus==0){
            // alert("商品未发货，请耐心等待");
        }else if(pStatus==3){
            // alert("商品已收货，请勿重复收货");
        }else if(pStatus==4){
            // alert("交易完成，无法收货");
        }else if(pStatus==5||pStatus==6||pStatus==7){
            // alert("商品正在退货中，无法收货");
        }

    }

    function addMoney(addAmount, uid) {
        $.ajax({
            url: "/user/addMoney",
            type: "POST",
            data: "addAmount=" + addAmount + "&uid=" + uid,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("用户退款成功");
                } else {
                    alert("用户退款失败");
                }
            },
            error: function (xhr) {
                alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }


</script>
</body>

</html>